//
//  TXFNetworkTools.swift
//  weibo飞飞
//
//  Created by 田侠飞 on 16/6/26.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

import UIKit
import AFNetworking
//定义枚举

enum MethodType : String {

case GET  = "GET"
    
case POST = "POST"



}
class TXFNetworkTools: AFHTTPSessionManager {
//设置单例
    static let  shareTools : TXFNetworkTools = {
    
    
    let tools = TXFNetworkTools()
        
      tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
    return tools
    
    
    }()
    
 //设置get 和 post方法
    
    func  request(method: MethodType,urlString:String,parameters: AnyObject?,success: (response: AnyObject?)->(),failure:(error:NSError)->()){
    
    
        if method  == MethodType.GET {
        
        //根据afn获取网路数据方法
            
       self.GET(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
        
           success(response: res)
        
        }, failure: { (_, err) -> Void in
            
            failure(error: err)
       })
        }else {
        
        
        self.POST(urlString, parameters: parameters, progress: nil, success: { (_, res) -> Void in
            success(response: res)
            }, failure: { (_, err) -> Void in
                
                failure(error: err)
            
        })
    }
   }
   
}

extension TXFNetworkTools{
    
    //发表评论:
    
    func sendWBCommentsData(comment: String ,success: (response: AnyObject?)->(),failure:(error:NSError)->()){
        
        let urlString = "https://api.weibo.com/2/comments/create.json"
        
       
        
        let param = [
            
            "access_token" : TXFUserAccountTools.sharedTools.accessToken!,
            
            
//            "id":  id,
            
            "comment": comment
            
//            "comment_ori": "\(comment_ori)"
        ]
        
        
        self.request(.POST, urlString: urlString, parameters: param, success: success, failure: failure)
        
        
    }

    
    
//发送图片微博
    
    func sendWBImageData(status: String ,imageList:[UIImage],success: (response: AnyObject?) -> (),failure:(error: NSError) ->()){
    
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        
        
        let param = [
            
            "access_token" : TXFUserAccountTools.sharedTools.accessToken!,
            
            
            "status": status
        ]

    
        self.POST(urlString, parameters: param, constructingBodyWithBlock: { (fromData) -> Void in
            
            //数据转换:
            
            for image  in imageList{
            
            
            let  data  = UIImagePNGRepresentation(image)!
                
            fromData.appendPartWithFileData(data, name: "pic", fileName: "xx", mimeType: "application/octet-stream")
            
            
            
            
            }
            
            
            
            
            
            
            }, progress: nil, success: { (_, response) -> Void in
                
               success(response: response)
                
            }) { (_, error) -> Void in
            
                
                failure(error: error)
        }
        
        
        
    
    }
    
   
    
    
//发送微博:
    
    func sendWBData(status: String ,success: (response: AnyObject?)->(),failure:(error:NSError)->()){
     let urlString = "https://api.weibo.com/2/statuses/update.json"
    
     let param = [
            
            "access_token" : TXFUserAccountTools.sharedTools.accessToken!,
            
            
            "status": status
        ]
        

      self.request(.POST, urlString: urlString, parameters: param, success: success, failure: failure)
    
    
    }
    
    
    
    
//主页未读微博数据
    func unReadCountData( success: (response: AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
//        access_token	false	string	采用OAuth授权方式为必填参数，其他授权方式不需要此参数，OAuth授权后获得。
//        uid	true	int64	需要获取消息未读数的用户UID，必须是当前登录用户。
//        callback	false	string	JSONP回调函数，用于前端调用返回JS格式的信息。
//        unread_message	false	boolean	未读数版本。0：原版未读数，1：新版未读数。默认为0。
        let param = [
            
            "access_token" : TXFUserAccountTools.sharedTools.accessToken!,

            
            "unread_message": 0
        ]
        
        
        self.request(.GET, urlString: urlString, parameters: param, success: success, failure: failure)
        
        
    }
    

    
//主页数据:
    func homeLoadUserInfo(since_id: Int64,max_id: Int64, success: (response: AnyObject?)->(),failure:(error:NSError)->()){
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        since_id	false	int64	若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。下拉刷新
//        max_id	false	int64	若指定此参数，则返回ID小于或等于max_id的微博，默认为0。上拉刷新
        let param = [
            
            "access_token" : TXFUserAccountTools.sharedTools.accessToken!,
            
            "since_id" : "\(since_id)",
            "max_id" : "\(max_id)"
        ]
       

    self.request(.GET, urlString: urlString, parameters: param, success: success, failure: failure)
    
    
    }
    
    
    
//个人信息数据:
    func oauthLoadUserAccount(code:String,success: (response: AnyObject?)->(),failure:(error:NSError)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            
            "client_id" : APPKEY,
            "client_secret":APPSecret ,
            "grant_type" : "authorization_code" ,
            
            "code":code ,
            "redirect_uri":REDIRECT_URI
            
        ]
        
        
        //调用方法:
        
        self.request(MethodType.POST, urlString: urlString, parameters: parameters, success: success, failure: failure)
        
    }
    
    func oauthLoadUserInfo(userAccount: TXFUserAccount , success: (response: AnyObject?)->(),failure:(error:NSError)->()){
    
    let urlString = "https://api.weibo.com/2/users/show.json"
    
    let parameters = [
        
        "access_token" :userAccount.access_token!,
        
        "uid":userAccount.uid!
        ]
        
        
        self.request(MethodType.GET, urlString: urlString, parameters: parameters, success: success, failure: failure)
    }
    
 

}

